package com.shanghaionstar.smart;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.smart.GetSmartDeviceResponse;
import com.onstar.cn.weight.ProgressLayout;
import com.onstar.cn.weight.ToastView;
import com.shanghaionstar.R;
import com.shanghaionstar.smart.adapter.SmartGeoAdapter;
import com.shanghaionstar.smart.iview.ISmartTriggerView;
import com.shanghaionstar.smart.presenter.SmartTriggerPresenter;
import com.shanghaionstar.user.adapter.ViewHolder;
import com.shanghaionstar.utils.AlertDialogUtil;

import java.util.HashSet;
import java.util.List;

/**
 * Created by Rengui Lu on 2016/6/12.
 */
public class SmartTriggerFrg extends BaseFragment implements View.OnClickListener,ISmartTriggerView{
    @FindViewById(R.id.title_tv)
    TextView mTitleTv;
    @FindViewById(R.id.home_rl)
    RelativeLayout mHomeRl;
    @FindViewById(R.id.geo_rl)
    RelativeLayout mGeoRl;
    @FindViewById(R.id.devices_gv)
    GridView mDevicesGv;
    @FindViewById(R.id.scan_pl)
    ProgressLayout mScanPl;

    private TextView mHomeTitleTv;
    private TextView mHomeDesTv;
    private TextView mGeoTitleTv;
    private TextView mGeoDesTv;
    private ImageView mArrowIv;

    private SmartTriggerPresenter presenter;
    private SmartGeoAdapter mAdapter;
    private int itemClickPos = 0;


    private BaseActivity mActivity;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_slk_trigger_setting;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = (BaseActivity) activity;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (isFirstLoad()) {
            initView(view);
            presenter = new SmartTriggerPresenter(mActivity.getScheduler());
            presenter.setView(this);
            presenter.getSmartDevices();
            presenter.setAddress();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mActivity instanceof SmartHomeActivity) {
            mGeoDesTv.setText(((SmartHomeActivity) mActivity).distance);
        }
//        presenter.setAddress();
    }

    private void initView(View view) {
        mHomeTitleTv = (TextView) mHomeRl.findViewById(R.id.title_tv);
        mHomeDesTv = (TextView) mHomeRl.findViewById(R.id.content_tv);
        mGeoTitleTv = (TextView) mGeoRl.findViewById(R.id.title_tv);
        mGeoDesTv = (TextView) mGeoRl.findViewById(R.id.content_tv);
        mTitleTv.setText(R.string.smart_trigger_setting);
        mHomeTitleTv.setText(R.string.smart_family);
        mGeoTitleTv.setText(R.string.smart_geo_fence_radius);
        mGeoDesTv.setText(R.string.smart_one_k);
        mArrowIv = (ImageView) mHomeRl.findViewById(R.id.arrow_iv);
    }

    @OnClick({R.id.home_rl, R.id.back_ib, R.id.geo_rl, R.id.submit_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.home_rl:
//                goToNavSearchActivity();
                presenter.goToSetAddress();
                break;
            case R.id.geo_rl:
                presenter.switchToSelDistanceFrg();
                break;
            case R.id.back_ib:
                back();
                break;
            case R.id.submit_btn:
                HashSet<Object> values = mAdapter.getmSet();
                if(values != null && !values.isEmpty()) {
                    presenter.batchSmartGeoFencing(values);
                }else{
                    new ToastView(mActivity).showToast(getString(R.string.smart_not_select_device));
                }
                break;
        }
    }

    public void setHomeAddr(PoiItemInfo itemInfo){
        if(itemInfo == null)
            return;
        presenter.saveHomeAddress(itemInfo);
        String address = itemInfo.getTitle();
        setHomeAddressView(address);
//        mHomeDesTv.setText(address);
    }

    private void setHomeAddressView(String address) {
        if(!TextUtils.isEmpty(address)) {
            mHomeDesTv.setText(address);
            mArrowIv.setVisibility(View.GONE);
            mHomeRl.setClickable(false);
        }
    }

//    private void showWarmDia() {
//        AlertDialogUtil.showConfigureDialog(mActivity,
//                mActivity.getString(R.string.mro_confirm),
//                mActivity.getString(R.string.smart_no_select_device),
//                mActivity.getString(R.string.cancel),
//                new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//
//                    }
//                });
//    }

//    @Override
//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if(resultCode == 1) {
//            Object obj = data.getSerializableExtra("result");
//            if(obj instanceof SetAddressResult) {
//                SetAddressResult setAddressResult = (SetAddressResult)obj;
//                if(setAddressResult.mapChoosePoiType == MapChoosePoiType.SMART) {
//                    mHomeDesTv.setText(setAddressResult.poiItemInfo.getTitle());
//                }
//            }
//        }
//    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void setLoading(boolean isLoading) {
        mScanPl.setVisibility(isLoading?View.VISIBLE:View.GONE);
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showDialogByOnstarException(mActivity,e);
    }

    @Override
    public void onReponseSuccess(Object object) {
        back();
    }

    @Override
    public Activity onActivity() {
        return mActivity;
    }

    @Override
    public void initData(List mSmartDevices) {
        mAdapter = new SmartGeoAdapter<GetSmartDeviceResponse>(mActivity, mSmartDevices, R.layout.fragment_trigger_gv_item) {
            @Override
            public void convert(ViewHolder holder, GetSmartDeviceResponse response, int pos) {
                String deviceName = response.deviceName;
                holder.setText(R.id.device_item_tv, TextUtils.isEmpty(deviceName) ? mContext.getString(R.string.smart_default_name) : deviceName);
                holder.setTextColor(R.id.device_item_tv, getResources().getColor(R.color.smart_pg));
                holder.setBackgroundRes(R.id.device_item_tv, R.drawable.smart_add_dev_btn);
                for (int clickPos : getmSet()) {
                    if (clickPos == pos) {
                        holder.setTextColor(R.id.device_item_tv, getResources().getColor(R.color.white));
                        holder.setBackgroundRes(R.id.device_item_tv, R.drawable.smart_item_btn);
                    }
                }
            }
        };
        mDevicesGv.setAdapter(mAdapter);
        mDevicesGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                itemClickPos = i;
                mAdapter.checkClick(i);
                mAdapter.notifyDataSetChanged();
            }
        });
    }


    @Override
    public void setHomeAddress(String address) {
        setHomeAddressView(address);
    }
}
